package com.cloudcc.mobile.dao;

import com.cloudcc.cloudframe.net.ErrorInfo;
import com.cloudcc.cloudframe.net.async.AsyncClient;
import com.cloudcc.cloudframe.net.async.BeanRequest;
import com.cloudcc.cloudframe.util.ConnectivityUtils;
import com.cloudcc.cloudframe.util.GsonUtil;
import com.cloudcc.mobile.AppConstant;
import com.cloudcc.mobile.AppContext;
import com.cloudcc.mobile.manager.RunTimeManager;
import com.cloudcc.mobile.manager.UrlManager;
import com.loopj.android.http.RequestHandle;
import com.loopj.android.http.RequestParams;
import com.loopj.android.http.ResponseHandlerInterface;

public class BaseEngine {

	/**
	 * 判断数据库插入 跟更新操作 是不是 成功
	 * 
	 * @param index
	 * @return
	 */
	public boolean isUpdateOk(long index) {
		if (index >= 0)
			return true;
		return false;
	}

	/**
	 * get请求
	 * 
	 * @param url
	 * @param request
	 */
	public <T> void sendGet(String url, BeanRequest<T> request) {
		sendGet(url, request);
	}

	/**
	 * get 请求
	 * 
	 * @param url
	 * @param request
	 */
	public void sendGet(String url, ResponseHandlerInterface request) {
		// 拦截器
		if (interceptorRequest(request)) {
			AsyncClient.getInstance().get(url, request);
		}
	}
	/**
	 * @param url 
	 * @param params
	 * @param request
	 */
	public void sendGet(String url,RequestParams params,ResponseHandlerInterface request) {
		// 拦截器
		if (interceptorRequest(request)) {
			AsyncClient.getInstance().get(url, params, request);
		}
	}

	public <T> void sendGetSync(String url, BeanRequest<T> request) {
		if (interceptorRequest(request)) {
			AsyncClient.getInstance().getSyncClient().get(url, request);
		}
	}
	public <T> void sendGetSync(String url, RequestParams params,BeanRequest<T> request) {
		if (interceptorRequest(request)) {
			AsyncClient.getInstance().getSyncClient().get(url, params, request);
		}
	}
	

	/**
	 * 默认接口 默认请求 data json数据转换 以及serviceName 的预先设定
	 * 
	 * @param param
	 *            将要转换的 object对象
	 * @param request
	 *            请求的控制器
	 * @param serviceName
	 *            服务名
	 * @param hasBinding
	 *            是否自动绑定
	 */
	public RequestHandle sendPost(Object param,
			ResponseHandlerInterface request, String serviceName,
			boolean hasBinding) {
		String data = GsonUtil.Object2Json(param);
		RequestParams params = new RequestParams();
		params.put("data", data);
		params.put("serviceName", serviceName);
		if (hasBinding && params != null) {
			params.put("binding", RunTimeManager.getInstance().getServerBinding());
		}
		return sendPost(UrlManager.getInterfaceUrl(), params, request);
	}

	/**
	 * 自动添加 bindding 跟servicename的 post方法
	 * 
	 * @param params
	 *            参数 可以null
	 * @param serviceName
	 *            请求 服务名
	 * @param request
	 *            请求
	 * @return
	 */
	protected RequestHandle sendPost(RequestParams params, String serviceName,
			ResponseHandlerInterface request) {
		if (params == null) {
			params = new RequestParams();
		}
		params.put("serviceName", serviceName);
		return sendPost(UrlManager.getInterfaceUrl(), params, request, true);
	}

	/**
	 * 可以自动添加 binding 字段
	 * 
	 * @param url
	 *            连接
	 * @param params
	 *            参数
	 * @param request
	 *            请求 返回
	 * @param hasBinding
	 *            是否需要添加binding字段 是否添加bingding字段 如果 true 就添加 false 不添加
	 */
	protected RequestHandle sendPost(String url, RequestParams params,
			ResponseHandlerInterface request, boolean hasBinding) {
		if (hasBinding && params != null) {
			params.put("binding", RunTimeManager.getInstance().getServerBinding());
		}
		return sendPost(url, params, request);
	}

	/**
	 * 发送post 请求
	 * 
	 * @param url
	 *            url连接
	 * @param params
	 *            参数
	 * @param request
	 *            请求request
	 */
	protected RequestHandle sendPost(String url, RequestParams params,
			ResponseHandlerInterface request) {
		if (interceptorRequest(request)) {
			return AsyncClient.getInstance().post(url, params, request);
		}
		return null;
	}

	/**
	 * 请求拦截器
	 * 
	 * @param request
	 *            http请求控制器
	 * @return 通过 true 没通过 false
	 */
	private final boolean interceptorRequest(ResponseHandlerInterface request) {
		if (ConnectivityUtils.hasNoInternet(AppContext.getInstance())) {// 没有网络
			request.sendFailureMessage(ErrorInfo.NET_INTERCEPTOR, null, null,new Throwable(String.valueOf(ErrorInfo.NET_NO_NETWORK)));
			return false;
		}
		return true;
	}
	
	/** 查询对象
	 * @param objectApiName
	 * @param serviceName
	 * @param page
	 * @param pageSize
	 * @param expressions
	 * @param request
	 */
	protected void queryObjAPi(String objectApiName,String serviceName,int page,int pageSize,String expressions,ResponseHandlerInterface request) {
		RequestParams params = new RequestParams();
		params.put("serviceName",serviceName);
		params.put("pageSize",AppConstant.PAGE_SIZE);
		params.put("pageNUM",page);
		params.put("objectApiName",objectApiName);
		params.put("binding",RunTimeManager.getInstance().getServerBinding());
		params.put("expressions",expressions);
		sendPost(UrlManager.getInterfaceUrl(), params, request);
	}
	/** 查询 对象 待权限
	 * @param objectApiName
	 * @param page
	 * @param expressions
	 * @param request
	 */
	protected void queryObjAPiWithRole(String objectApiName,int page,String expressions,ResponseHandlerInterface request) {
		queryObjAPi(objectApiName, "pageQueryWithRoleRight", page, AppConstant.PAGE_SIZE, expressions, request);
	}
	/** 根据对象单个的id 查询 对象的数据
	 * @param objectApiName 对象apiname
	 * @param id 对象单条记录的id
	 * @param request 
	 */
	protected void queryObjAPiWithRoleById(String objectApiName,String id,ResponseHandlerInterface request) {
		queryObjAPi(objectApiName, "cquery", 1,  AppConstant.PAGE_SIZE, "id='"+id+"'", request);
	}

}
